define(function (require) {
    require('table');

    $.module("BASE.Company", function () {

        return {
            init: function () {
                this.loadData();
                this.loadEvent();
            },
            clearDatas: function () {
                var that = this;

                $("#_id").val(0);

                $('#fm').bootstrapValidator('resetForm', true);
                that.showDiv('main_div');
            },
            loadData: function (pageNumber, pageSize) {
                var that = this;

                let s_FID = $("#s_FID").val();
                let s_FNumber = $("#s_FNumber").val();
                let s_FName = $("#s_FName").val();
                let s_Active = $("#s_Active").val();

                SYS.Core.ajax({
                    url: "base/company/list",
                    common: {
                        page: pageNumber ? pageNumber : 1,
                        size: pageSize ? pageSize : 20
                    },
                    data: {
                        Id: s_FID,
                        Code: s_FNumber,
                        Name: s_FName,
                        Actived: s_Active || -1
                    },
                    success: function (data) {
                        var obj = {
                            'pageNumber': data.data.extra.current_page,
                            'pageSize': data.data.extra.page_size,
                            'totalRows': data.data.extra.all_count,
                            'data': data.data.list
                        };

                        $("#myTable").bootstrapTable('destroy');
                        $('#myTable').bootstrapTable({
                            striped: true,
                            singleSelect: false,
                            showColumns: true,
                            showToggle: true,
                            pagination: true,
                            pageNumber: obj.pageNumber,
                            pageSize: obj.pageSize,
                            totalRows: obj.totalRows,
                            pageList: [20, 50, 100, 200],
                            sidePagination: 'server',
                            clickToSelect: true,
                            idField: 'id',
                            data: obj.data,
                            columns: [
                                {field: 'Id', title: '公司ID', align: 'center'},
                                {field: 'Code', title: '公司编码', align: 'center'},
                                {field: 'Name', title: '公司名称', align: 'center'},
                                {field: 'ParentId', title: '上级公司', align: 'center'},
                                {field: 'ParentName', title: '上级公司名称', align: 'center'},
                                {
                                    field: 'FIsLeaf', title: '是否叶子节点', align: 'center',
                                    formatter: function (value, row, index) {
                                        return value == 0 ? '<label class="text-danger">否</label>' : '<label class="text-success">是</label>';
                                    }
                                },
                                {
                                    field: 'FIsGroup', title: '是否为集团公司', align: 'center',
                                    formatter: function (value, row, index) {
                                        return value == 0 ? '<label class="text-danger">否</label>' : '<label class="text-success">是</label>';
                                    }
                                },
                                {
                                    field: 'FIsOnlyUnion', title: '是否仅用于集团合并', align: 'center',
                                    formatter: function (value, row, index) {
                                        return value == 0 ? '<label class="text-danger">否</label>' : '<label class="text-success">是</label>';
                                    }
                                },
                                // {field: 'BelongTo', title: '管理口径归属', align: 'center'},
                                {
                                    field: 'Active', title: '启用的', align: 'center',
                                    formatter: function (value, row, index) {
                                        return value == 0 ? '不启用' : '启用';
                                    }
                                },
                                // {
                                //     field: 'do', title: '操作', align: 'center',
                                //     formatter: function (value, row, index) {
                                //         var html = '';
                                //         html += '<a href="javascript:void(0)" onclick="BASE.Company.toEdit(' + row.id + ')" class="btn btn-edit">编辑</a> | ';
                                //         html += '<a href="javascript:void(0)" onclick="BASE.Company.toRemove(' + row.id + ')" class="btn btn-remove  ">删除</a>';
                                //         return html;
                                //     }
                                // }
                            ],
                            onPageChange: function (number, size) {
                                that.loadData(number, size);
                            },
                            onRefreshTable: function () {
                                that.loadData();
                            },
                            onClickCell: function (field, value, row, $element) {
                            }
                        });
                    }
                });
            },
            loadEvent: function () {
                var that = this;

                // 定义编辑框form
                var $form = $("#fm");

                // 查询
                $("#search-toolbar").on('click', "#btn_search", function () {
                    that.loadData();
                }).on("keydown ", "input", function (e) {
                    if (e.keyCode == 13) {
                        that.loadData();
                    }
                });

                // 返回
                $(".back_list").on('click', function () {
                    that.clearDatas();
                    that.showDiv('main_div');
                });
            },
            toEdit: function (id) {
                var that = this;

                that.clearDatas();
                $("#btn-add-div").hide();
                $("#btn-edit-div").show();

                SYS.Core.ajax({
                    url: 'base/company/detail',
                    common: {},
                    data: {
                        FID: id ? id : 0
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            let obj = data.data;

                            that.showDiv('edit_div');
                        }
                    }
                });
            },
            showDiv: function (div) {
                var divs = ['main_div', 'edit_div', 'import_div'];
                for (var i = 0; i < divs.length; i++) {
                    $("#" + divs[i]).hide();
                }
                $("#" + div).show();
            }
        }
    });
    BASE.Company.init();
});